//
//  ProjectNetworkManager.m
//  JFCloundChat
//
//  Created by HuanGe on 2021/11/1.
//

#import "ProjectNetworkManager.h"

@interface ProjectNetworkManager (){
    dispatch_queue_t _netQueue;
}
@end

@implementation ProjectNetworkManager

static ProjectNetworkManager *_manager = nil;

#pragma mark ---------------------------------- 初始化 类

- (instancetype)init
{
    self = [super init];
    if (self) {
        _netQueue = dispatch_queue_create("com.ilongge.fifm.netmanager", DISPATCH_QUEUE_SERIAL);
    }
    return self;
}

+ (instancetype)shareManager
{
    if (_manager == nil) {
        _manager = [[super alloc]init];
    }
    return _manager;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_manager == nil) {
            _manager = [super allocWithZone:zone];
        }
    });
    
    return _manager;
}

- (id)copyWithZone:(nullable NSZone *)zone {
    return _manager;
}

- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return _manager;
}

#pragma mark ---------------------------------- 初始化 请求类
 

- (void)netWorkRequestWithType:(RequestSerializerType)serializerType
                        andUrl:(NSString *)urlString
                  andParamters:(NSDictionary *)paramters
            andResponseHandler:(ResponseBlock)responseHandler
{
    if (paramters == nil ) {
        paramters = [NSDictionary dictionary];
    }
    dispatch_async(self->_netQueue, ^{
        NSURLSessionTask *task;
        if (serializerType == RequestSerializerTypeGet) {
            NSMutableString *string = [NSMutableString string];
            for (NSString *key in paramters.allKeys) {
                NSString *value = paramters[key];
                [string appendFormat:@"%@=%@", key, value];
            }
            NSURLSession *session = [NSURLSession sharedSession];
            NSString *finalString = [NSString stringWithFormat:@"%@?%@", urlString, string];
            NSURL *url = [NSURL URLWithString:finalString];
            task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
                NSDictionary *responseDic;
                if(!error){
                    responseDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (responseHandler) {
                        responseHandler(responseDic, error==nil, error.localizedDescription);
                    }
                });
            }];
        }
        if (task) {
            [task resume];
        }
    });
}

+ (void)netWorkRequestWithType:(RequestSerializerType)serializerType
                        andUrl:(NSString *)urlString
                  andParamters:(NSDictionary *)paramters
            andResponseHandler:(ResponseBlock)responseHandler
{
    [[ProjectNetworkManager shareManager] netWorkRequestWithType:serializerType
                                                          andUrl:urlString
                                                    andParamters:paramters
                                              andResponseHandler:responseHandler];
}
@end
